﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using LotteCinema.BUS;
//using DienTu.DAO;
using LotteCinema.DTO;

namespace LotteCinema.Admin
{
    public partial class SanPham : System.Web.UI.Page
    {
        public static bool _them;
        public static BPHIM bp = new BPHIM();
        public static DAO.CPhim cp = new DAO.CPhim();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    Session["folder"] = "PHIM";
                    LayTatCaPhim();
                    DataTable dt = bp.PHIM_LayTheLoaiPhim();
                    ddlTheLoaiPhim.DataSource = dt;
                    ddlTheLoaiPhim.DataValueField = "MATL";
                    ddlTheLoaiPhim.DataTextField = "TENTL";
                    ddlTheLoaiPhim.DataBind();
                    pnShow.Visible = true;
                    pnCapNhat.Visible = false;
                    pnThemTheLoaiPhim.Visible = false;
                }
                catch (Exception ex)
                {
                    DTO.WebMegBox.Show(ex.Message.ToString());
                }
            }
        }
        protected void KhoiTaoDuLieu()
        {
            DataTable dt=bp.PHIM_LayDinhDangPhim();
            ddlMaDD.DataSource = dt;
            ddlMaDD.DataValueField = "MADD";
            ddlMaDD.DataTextField = "TENDD";
            ddlMaDD.DataBind();
            dt = bp.PHIM_LayQuocGia();
            ddlMaQG.DataSource = dt;
            ddlMaQG.DataValueField = "MAQG";
            ddlMaQG.DataTextField = "TENQG";
            ddlMaQG.DataBind();
            txtTenPhim.Text = "";
            txtHangPhim.Text = "";
            txtGioiThieu.Text = "";
            txtDienVien.Text = "";
            txtDaoDien.Text = "";
            Session["Upload1"] = null;
            txtThoiLuong.Text = "";
            txtthoigiankc.Text = "";
            txtthoigiannc.Text = "";
            int i;
            for ( i = DateTime.Now.Year - 20; i <= DateTime.Now.Year+1; i++)
                ddlNam.Items.Add(i.ToString());
        }
        public void LayTatCaPhim()
        {
            try
            {
                grdPhim.DataSource = bp.PHIM_LayTatCaDL();
                grdPhim.DataBind();
            }
            catch (Exception ex)
            {
                DTO.WebMegBox.Show(ex.Message.ToString());
            }
        }
        protected void lbtThemMoi_Click(object sender, EventArgs e)
        {
            pnCapNhat.Visible = false;
            pnThemTheLoaiPhim.Visible = true;
          
        }
        protected void lbtCapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                cp._tenp = txtTenPhim.Text;
                cp._namsx = ddlNam.SelectedValue == cp._namsx ? cp._namsx : ddlNam.SelectedValue;
                cp._maqg = ddlMaQG.SelectedValue == cp._maqg ? cp._maqg : ddlMaQG.SelectedValue;
                cp._hangphim = txtHangPhim.Text;
                cp._gioithieu = txtGioiThieu.Text;
                cp._poster = Session["upLoad"] == null ? "" : Session["upLoad"].ToString();
                cp._thoiluong = txtThoiLuong.Text;
                cp._madd = ddlMaDD.SelectedValue == cp._madd ? cp._madd : ddlMaDD.SelectedValue;
                cp._dienvien = txtDienVien.Text;
                cp._daodien = txtDaoDien.Text;
                cp._thoigiankc = txtthoigiankc.Text;
                cp._thoigiannc = txtthoigiannc.Text;
                bp.Phim_CapNhat(cp);
                grdTheLoaiPhim.DataSource = bp.PHIM_LayTatCaChiTietTheLoai(cp);
                grdTheLoaiPhim.DataBind();
                //DataTable dt = bp.PHIM_LayTheLoaiPhim();
                //ddlTheLoaiPhim.DataSource = dt;
                //ddlTheLoaiPhim.DataValueField = "MATL";
                //ddlTheLoaiPhim.DataTextField = "TENTL";
                ddlTheLoaiPhim.DataBind();
                pnCapNhat.Visible = false;
                pnThemTheLoaiPhim.Visible = true;
                lbtheloaiphim.Visible = false;
                lblThemTheLoaiPhim.Visible = false;
                ddlTheLoaiPhim.Visible = false;
                lblCapNhatTheLoaiPhim.Visible = false;
            }
            catch (Exception ex)
            {
                DTO.WebMegBox.Show(ex.Message.ToString());
            }

        }
        protected void grdPhim_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            try
            {
                cp._maphim = e.CommandArgument.ToString();
                if (e.CommandName == "Edit")
                {
                    if (bp.Phim_KTPhimDaCoVe(cp) == 0)
                    {
                        int i = 0;
                        KhoiTaoDuLieu();
                        txtTenPhim.Text = e.Item.Cells[1].Text == "&nbsp;" ? "" : e.Item.Cells[1].Text;
                        cp._namsx = e.Item.Cells[2].Text;
                        while (ddlNam.Items[i].Text != cp._namsx)
                        {
                            i++;
                        }
                        ddlNam.Items[i].Selected = true;
                        cp._maqg = e.Item.Cells[3].Text;
                        i = 0;
                        while (ddlMaQG.Items[i].Value != cp._maqg)
                        {
                            i++;
                        }
                        ddlMaQG.Items[i].Selected = true;
                        txtHangPhim.Text = e.Item.Cells[5].Text == "&nbsp;" ? "" :e.Item.Cells[5].Text;
                        txtGioiThieu.Text = e.Item.Cells[6].Text == "&nbsp;" ? "" :e.Item.Cells[6].Text;
                        Session["upLoad"] = e.Item.Cells[7].Text == "&nbsp;" ? "" :e.Item.Cells[7].Text;
                        txtThoiLuong.Text = e.Item.Cells[8].Text == "&nbsp;" ? "" :e.Item.Cells[8].Text;
                        cp._madd = e.Item.Cells[9].Text;
                        i = 0;
                        while (ddlMaDD.Items[i].Value != cp._madd)
                        {
                            i++;
                        }
                        ddlMaDD.Items[i].Selected = true;
                        txtDienVien.Text = e.Item.Cells[11].Text == "&nbsp;" ? "" :e.Item.Cells[11].Text;
                        txtDaoDien.Text = e.Item.Cells[12].Text == "&nbsp;" ? "" : e.Item.Cells[12].Text;
                        txtthoigiankc.Text = e.Item.Cells[13].Text;
                        txtthoigiannc.Text = e.Item.Cells[14].Text;
                        pnShow.Visible = false;
                        pnCapNhat.Visible = true;
                        lbtThem.Visible = false;
                        lbtCapNhat.Visible = true;
                        lblCapNhatTheLoaiPhim.Visible = true;
                    }
                }
                else
                {
                    if (e.CommandName == "Delete" && bp.Phim_KTPhimDaCoVe(cp) == 0)
                    {
                        try
                        {
                            bp.Phim_Xoa(cp);
                            grdPhim.DataSource =bp.PHIM_LayTatCaDL();
                            grdPhim.DataBind();
                        }
                        catch (Exception ex)
                        {
                            DTO.WebMegBox.Show(ex.Message.ToString());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                DTO.WebMegBox.Show(ex.Message.ToString());
            }
           
        }
        protected void grdTheLoaiPhim_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            lblThemTheLoaiPhim.Visible = false;
            if (e.CommandName == "Edit")
            {
                cp._maphim = e.CommandArgument.ToString();
                cp._matl = e.Item.Cells[1].Text;
                lbtheloaiphim.Visible = true;
                ddlTheLoaiPhim.Visible = true;
                lblCapNhatTheLoaiPhim.Visible = true;
            }
        }
        protected void lblQuayLai_Click(object sender, EventArgs e)
        {
            LayTatCaPhim();
            pnShow.Visible = true;
            pnCapNhat.Visible = false;
            pnThemTheLoaiPhim.Visible = false;
        }
        protected void grdPhim_PhanTrang(object source, DataGridPageChangedEventArgs e) // Phân Trang 
        {
            try
            {
                grdPhim.CurrentPageIndex = +e.NewPageIndex;
                LayTatCaPhim();
            }
            catch (Exception ex)
            {
                DTO.WebMegBox.Show(ex.Message.ToString());
            }
        }

        protected void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                KhoiTaoDuLieu();
                pnCapNhat.Visible = true;
                pnShow.Visible = false;
                lbtCapNhat.Visible = false;
            }
            catch (Exception ex)
            {
                DTO.WebMegBox.Show(ex.Message.ToString());
            }
        }
      
        protected void lblCapNhatTheLoaiPhim_Click(object sender, EventArgs e)
        {
            try
            {
                string matlthem = ddlTheLoaiPhim.SelectedValue;
                bp.Phim_CapNhatTheLoai(cp, matlthem);
                grdTheLoaiPhim.DataSource = bp.PHIM_LayTatCaChiTietTheLoai(cp);
                grdTheLoaiPhim.DataBind();
                ddlTheLoaiPhim.Items.RemoveAt(ddlTheLoaiPhim.SelectedIndex);
                lbtheloaiphim.Visible = false;
                lblThemTheLoaiPhim.Visible = false;
                ddlTheLoaiPhim.Visible = false;
                lblCapNhatTheLoaiPhim.Visible = false;
            }
            catch(Exception ex)
            {
                DTO.WebMegBox.Show(ex.Message.ToString());
            }
        }

        protected void lblThemTheLoaiPhim_Click(object sender, EventArgs e)
        {
            try
            {
                cp._matl = ddlTheLoaiPhim.SelectedValue;
                bp.Phim_ThemTheLoai(cp);
                ddlTheLoaiPhim.Items.RemoveAt(ddlTheLoaiPhim.SelectedIndex);
                grdTheLoaiPhim.DataSource = bp.PHIM_LayTatCaChiTietTheLoai(cp);
                grdTheLoaiPhim.DataBind();
            }
            catch (Exception ex)
            {
                DTO.WebMegBox.Show(ex.Message.ToString());
            }
        }

        protected void lbtThem_Click(object sender, EventArgs e)
        {
            try
            {
                cp._tenp = txtTenPhim.Text;
                cp._namsx = ddlNam.SelectedValue;
                cp._maqg = ddlMaQG.SelectedValue;
                cp._hangphim = txtHangPhim.Text;
                cp._gioithieu = txtGioiThieu.Text;
                cp._poster = Session["upLoad"] == null ? "" : Session["upload"].ToString();
                cp._thoiluong = txtThoiLuong.Text;
                cp._madd = ddlMaDD.SelectedValue;
                cp._dienvien = txtDienVien.Text;
                cp._daodien = txtDaoDien.Text;
                cp._thoigiankc = txtthoigiankc.Text;
                cp._thoigiannc = txtthoigiannc.Text;
                bp.Phim_Them_Fix(cp);
                grdTheLoaiPhim.DataSource = bp.PHIM_LayTatCaChiTietTheLoai(cp);
                grdTheLoaiPhim.DataBind();
                pnCapNhat.Visible = false;
                pnThemTheLoaiPhim.Visible = true;
                lblCapNhatTheLoaiPhim.Visible = false;
            }
            catch (Exception ex)
            {
                DTO.WebMegBox.Show(ex.Message.ToString());
            }
       
        }

        protected void lblphimdangchieu_Click(object sender, EventArgs e)
        {
            try
            {
                grdPhim.DataSource =bp.PHIM_LayTatCaPhimDangChieu();
                grdPhim.DataBind();
            }
            catch (Exception ex)
            {
                DTO.WebMegBox.Show(ex.Message.ToString());
            }
        }

        protected void lblphimsapchieu_Click1(object sender, EventArgs e)
        {
            try
            {
                grdPhim.DataSource =bp.PHIM_LayTatCaPhimSapChieu();
                grdPhim.DataBind();
            }
            catch (Exception ex)
            {
                DTO.WebMegBox.Show(ex.Message.ToString());
            }
        }
    }
}